package edu.szte.ovrt.datahandler.ui.console;

import java.util.Map;

/**
 * Base class for Console Menu handling.
 * 
 * @author Péter Török
 */
public abstract class Menu extends UIBase implements IAction {

	/**
	 * Stores the title of the menu
	 */
	protected abstract String getTitle();

	/**
	 * Stores a dictionary for menu items
	 */
	protected final MenuMap m_menuMap = new MenuMap();

	/**
	 * Enters the menu, infinite loop
	 */
	public final void run() {
		try {
			do {
				showMenu();
				final int selection = readInt("Your selection");
				MenuItem menuItem;
				if ((menuItem = m_menuMap.get(selection)) != null) {
					menuItem.getAction().run();
				}
			} while (true);
		} catch (final ExitAction exit) {
			// NOOP
		}
	}

	/**
	 * Print a menu-selection to the screen TODO Hanlde the next(prev)-page function
	 */
	private void showMenu() {
		newScreen(getTitle());
		for (final Map.Entry<Integer, MenuItem> item : m_menuMap.entrySet()) {
			showNumberedLine(item.getKey(), item.getValue().getTitle());
		}

	}
}
